package com.marchsoft.musicalstore.util;

import org.springframework.stereotype.Component;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 源
 * Date: 2019-11-17
 * Time: 14:58
 */
@Component
public class MyUntil {
    /**
     * 将传入的密码进行md5加密
     * @param passpord
     * @return
     */
    MessageDigest md;


    public String mdString(String passpord) throws NoSuchAlgorithmException {
        md = MessageDigest.getInstance("MD5");
        md.update(passpord.getBytes());
        return new BigInteger(1, md.digest()).toString(16);
    }

    /**
     * 源计算两个日期之间的差值，第一个时间的格式是20191512，后期那个接口的逻辑可能会改，这个接口估计就没用了
     * @param oneTime
     * @param TwoTime
     */
    public void getTowDayDifference(int oneTime, Date TwoTime){
        Map<Integer,Integer> monthMap = new HashMap<>();
        monthMap.put(1,31);
        monthMap.put(2,28);
        monthMap.put(3,31);
        monthMap.put(4,30);
        monthMap.put(5,31);
        monthMap.put(6,30);
        monthMap.put(7,31);
        monthMap.put(8,31);
        monthMap.put(9,30);
        monthMap.put(10,31);
        monthMap.put(11,30);
        monthMap.put(12,31);
        int oneDay = oneTime%100;
        int oneMonth = (oneTime/100)%100;
        int oneYear = oneTime/10000;
    }

    /**
     * 判断两个时间先后，前者先返回1相等返回0
     * @param oneTime
     * @param TwoTime
     * @return
     */
    public int judgetwoTiom(int oneTime, Date TwoTime){
        int oneDay = oneTime%100;
        int oneMonth = (oneTime/100)%100;
        int oneYear = oneTime/10000;
        Calendar calendar = Calendar.getInstance();//这个东西貌似专门用来取时间的
        calendar.setTime(TwoTime);
        int year = calendar.get(Calendar.YEAR);         //获取年份
        int month = calendar.get(Calendar.MONTH);       //获取月份
        int day = calendar.get(Calendar.DATE);          //获取日
        if(oneYear<year){
            return 1;
        }else if(oneYear>year){
            return -1;
        }
        if(oneMonth<month){
            return 1;
        }else if(oneMonth>month){
            return -1;
        }
        if(oneDay<day){
            return 1;
        }else if(oneDay>day){
            return -1;
        }
        return 0;
    }

    /**
     * 源传入一个雷士20191512的时间，返回date类型
     * @param myTime
     * @return
     */
    public Date getDate(int myTime){
        int oneDay = myTime%100;
        int oneMonth = (myTime/100)%100;
        int oneYear = myTime/10000;
        Calendar calendar = Calendar.getInstance();
        calendar.set(oneYear,oneMonth,oneDay);
        Date time = calendar.getTime();
        return time;

    }

    /**
     * 源传入两个date算两个之间的查,在网上查的，算法好骚啊,优秀
     * @param date1
     * @param date2
     * @return
     */
    public long getTwoDateBetweenDay(Date date1, Date date2){
        Calendar c1=Calendar.getInstance();
        c1.setTime(date1);
        Calendar c2=Calendar.getInstance();
        c2.setTime(date2);
        long t1=c1.getTimeInMillis();
        long t2=c2.getTimeInMillis();
        long days=(t2-t1)/(24*60*60*1000);
        return days;
    }

}
